package com.sm.service;

import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm.dao.FirstSortMapper;
import com.sm.model.FirstSort;
import com.sm.model.Goods;
import com.sm.model.HappyRequest;
import com.sm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.dao.GoodsMapper;

/**
 *	商品service层
 * @author 60915
 */
@Service
public class GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Autowired
	private FirstSortMapper firstSortMapper;

	/**
	 *	根据搜索内容搜索商品
	 * @param searchContent		搜索内容
	 * @return
	 */
	public PageResult selectGoodsByCont(String searchContent,Integer currentPage,Integer pageSize){
		PageHelper.startPage(currentPage,pageSize);
		Page<Goods> goods = goodsMapper.selectGoodsByCont(searchContent);
		return new PageResult(goods.getTotal(),goods);
	}

	/**
	 *	通过商品id查询商品信息
	 * @param goodsId	商品id
	 * @return
	 */
	public Goods selectByPrimaryKey(Integer goodsId) {
		Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
		FirstSort firstSort = firstSortMapper.selectByPrimaryKey(goods.getFirstSortId());
		if (firstSort == null || firstSort.getIsCanBuy().equals(0)) {
			goods.setIsCanBuy(0); //不能购买
		} else if (firstSort.getIsCanBuy().equals(1)) {
			goods.setIsCanBuy(1); //可以购买
		}
		return goods;
	}


	/**
	 * 根据学期查商品
	 * @param term
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public PageResult listByTerm(Integer firstSortId,Integer term, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage, pageSize);
		Page<Goods> goods = goodsMapper.listByTerm(firstSortId,term);
		return new PageResult(goods.getTotal(),goods.getResult());
	}

	public int updateById(Goods goods) {
		return goodsMapper.updateByPrimaryKeySelective(goods);
	}
}
























